﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;

namespace YArchitech.Plumbing
{
	public partial class FormRefrigerantPipeCreate : HYBaseForm
	{
		public bool GasLiquidIntegration
		{
			get
			{
				return this.gasLiquidIntegration;
			}
			set
			{
				this.gasLiquidIntegration = value;
			}
		}

		public FormRefrigerantPipeCreate(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.doc = doc;
		}

		private void btnPipeSetting_Click(object sender, EventArgs e)
		{
			FormMultiLinePipeSetting formMultiLinePipeSetting = new FormMultiLinePipeSetting(this.doc);
			System.Drawing.Point location = base.Location;
			formMultiLinePipeSetting.Location = new System.Drawing.Point(location.X + 340, location.Y);
			formMultiLinePipeSetting.ShowDialog();
		}

		private void FormRefrigerantPipeCreate_Load(object sender, EventArgs e)
		{
			this.SetdgvComboboxInfo();
			this.SetRefrigerantPipeInfo();
		}

		private void SetdgvComboboxInfo()
		{
			ICollection<MEPSize> pipeSegmentInfo = this.GetPipeSegmentInfo();
			if (pipeSegmentInfo == null)
			{
				using (List<List<string>>.Enumerator enumerator = new ClassRefrigerantPipeCreateInfo().GetPipeStorage().GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						List<string> list = enumerator.Current;
						this.PipeDiameter.Items.Add(list[0].ToDouble(0.0).ToString());
					}
					return;
				}
			}
			foreach (MEPSize mepsize in pipeSegmentInfo)
			{
				double num = Math.Round(mepsize.NominalDiameter * 304.8, 2, MidpointRounding.AwayFromZero);
				this.PipeDiameter.Items.Add(num.ToString());
			}
		}

		private ICollection<MEPSize> GetPipeSegmentInfo()
		{
			foreach (Element element in new OperateSegmentFitting(this.doc).GetAllPipeSegment())
			{
				PipeSegment pipeSegment = (PipeSegment)element;
				if (pipeSegment.Name == "铜 - 焊接")
				{
					return pipeSegment.GetSizes();
				}
			}
			return null;
		}

		private void SetRefrigerantPipeInfo()
		{
			List<List<string>> refrigerantPipeInfo = new ClassRefrigerantPipeCreateInfo().GetRefrigerantPipeInfo();
			int num = -1;
			foreach (List<string> list in refrigerantPipeInfo)
			{
				num++;
				this.dgvRefrigerantPipe.Rows.Add();
				for (int i = 0; i < list.Count; i++)
				{
					if (i == 1 && !this.PipeDiameter.Items.Contains(list[i]))
					{
						this.dgvRefrigerantPipe.Rows[num].Cells[i].Value = ((num == 0) ? "22.23" : "12.7");
					}
					else
					{
						this.dgvRefrigerantPipe.Rows[num].Cells[i].Value = list[i];
					}
				}
			}
			if (this.userSetting.GetRefrigerantPipeCreateText()[0] == "1")
			{
				this.rdbGasLiquidSeparate.Checked = true;
				this.gasLiquidIntegration = false;
				return;
			}
			this.rdbGasLiquidIntegration.Checked = true;
			this.gasLiquidIntegration = true;
		}

		private void rdbGasLiquidIntegration_CheckedChanged(object sender, EventArgs e)
		{
			this.GasLiquidIntegration = this.rdbGasLiquidIntegration.Checked;
			if (this.GasLiquidIntegration)
			{
				this.dgvRefrigerantPipe.Rows[1].Cells[2].ReadOnly = true;
				this.dgvRefrigerantPipe.Rows[1].Cells[3].ReadOnly = true;
				this.dgvRefrigerantPipe.Rows[1].Cells[2].Style.BackColor = System.Drawing.Color.LightGray;
				this.dgvRefrigerantPipe.Rows[1].Cells[3].Style.BackColor = System.Drawing.Color.LightGray;
				this.dgvRefrigerantPipe.Rows[1].Cells[2].Value = this.dgvRefrigerantPipe.Rows[0].Cells[2].Value;
				this.dgvRefrigerantPipe.Rows[1].Cells[3].Value = this.dgvRefrigerantPipe.Rows[0].Cells[3].Value;
				return;
			}
			this.dgvRefrigerantPipe.Rows[1].Cells[2].ReadOnly = false;
			this.dgvRefrigerantPipe.Rows[1].Cells[3].ReadOnly = false;
			this.dgvRefrigerantPipe.Rows[1].Cells[2].Style.BackColor = System.Drawing.Color.White;
			this.dgvRefrigerantPipe.Rows[1].Cells[3].Style.BackColor = System.Drawing.Color.White;
			this.dgvRefrigerantPipe.Rows[1].Cells[3].Value = this.dgvRefrigerantPipe.Rows[0].Cells[3].Value.ToString().ToDouble(0.0) - 200.0;
		}

		public List<double> Getoffset()
		{
			List<double> list = new List<double>();
			if (this.dgvRefrigerantPipe.Rows[0].Cells[3].Value == null || this.dgvRefrigerantPipe.Rows[1].Cells[3].Value == null)
			{
				YJKMessageBox.Show("输入不能为空！");
				this.dgvRefrigerantPipe.CurrentCell.Value = this.cellValue;
				return null;
			}
			if (!this.regex.IsMatch(this.dgvRefrigerantPipe.Rows[0].Cells[3].Value.ToString().Trim()) || !this.regex.IsMatch(this.dgvRefrigerantPipe.Rows[1].Cells[3].Value.ToString().Trim()))
			{
				YJKMessageBox.Show("水平偏移输入数字格式不正确,应为整数或小数，且不能为空！");
				this.dgvRefrigerantPipe.CurrentCell.Value = this.cellValue;
				return null;
			}
			list.Add(this.dgvRefrigerantPipe.Rows[0].Cells[3].Value.ToString().ToDouble(0.0));
			list.Add(this.dgvRefrigerantPipe.Rows[1].Cells[3].Value.ToString().ToDouble(0.0));
			return list;
		}

		public List<double> GetLevel()
		{
			List<double> list = new List<double>();
			if (this.dgvRefrigerantPipe.Rows[0].Cells[2].Value == null || this.dgvRefrigerantPipe.Rows[1].Cells[2].Value == null)
			{
				YJKMessageBox.Show("输入不能为空！");
				this.dgvRefrigerantPipe.CurrentCell.Value = this.cellValue;
				return null;
			}
			if (!this.regex.IsMatch(this.dgvRefrigerantPipe.Rows[0].Cells[2].Value.ToString().Trim()) || !this.regex.IsMatch(this.dgvRefrigerantPipe.Rows[1].Cells[2].Value.ToString().Trim()))
			{
				YJKMessageBox.Show("标高输入数字格式不正确,应为整数或小数，且不能为空！");
				this.dgvRefrigerantPipe.CurrentCell.Value = this.cellValue;
				return null;
			}
			list.Add(this.dgvRefrigerantPipe.Rows[0].Cells[2].Value.ToString().ToDouble(0.0));
			list.Add(this.dgvRefrigerantPipe.Rows[1].Cells[2].Value.ToString().ToDouble(0.0));
			return list;
		}

		public List<double> GetDiameter()
		{
			List<double> list = new List<double>();
			if (this.dgvRefrigerantPipe.Rows[0].Cells[1].Value == null || this.dgvRefrigerantPipe.Rows[1].Cells[1].Value == null)
			{
				YJKMessageBox.Show("输入不能为空！");
				this.dgvRefrigerantPipe.CurrentCell.Value = this.cellValue;
				return null;
			}
			if (!this.regex.IsMatch(this.dgvRefrigerantPipe.Rows[0].Cells[1].Value.ToString().Trim()) || !this.regex.IsMatch(this.dgvRefrigerantPipe.Rows[1].Cells[1].Value.ToString().Trim()))
			{
				YJKMessageBox.Show("管径格式不正确,应为正整数或小数，且不能为空！");
				this.dgvRefrigerantPipe.CurrentCell.Value = this.cellValue;
				return null;
			}
			list.Add(this.dgvRefrigerantPipe.Rows[0].Cells[1].Value.ToString().ToDouble(0.0));
			list.Add(this.dgvRefrigerantPipe.Rows[1].Cells[1].Value.ToString().ToDouble(0.0));
			return list;
		}

		private void dgvRefrigerantPipe_CurrentCellDirtyStateChanged(object sender, EventArgs e)
		{
			if (this.rdbGasLiquidIntegration.Checked)
			{
				this.dgvRefrigerantPipe.Rows[1].Cells[2].Value = this.dgvRefrigerantPipe.Rows[0].Cells[2].Value;
				this.dgvRefrigerantPipe.Rows[1].Cells[3].Value = this.dgvRefrigerantPipe.Rows[0].Cells[3].Value;
			}
			this.dgvRefrigerantPipe.CommitEdit(DataGridViewDataErrorContexts.Commit);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void FormRefrigerantPipeCreate_FormClosed(object sender, FormClosedEventArgs e)
		{
			try
			{
				int bVk = 27;
				int dwFlags = 0;
				int dwFlags2 = 2;
				int wMsg = 7;
				FormRefrigerantPipeCreate.SendMessage(RefrigerantPipeCreate.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				FormRefrigerantPipeCreate.keybd_event((byte)bVk, 0, dwFlags, 0);
				FormRefrigerantPipeCreate.keybd_event((byte)bVk, 0, dwFlags2, 0);
			}
			catch (Exception)
			{
			}
		}

		private void FormRefrigerantPipeCreate_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SetRefrigerantPipeCreateInfo();
		}

		private void SetRefrigerantPipeCreateInfo()
		{
			List<List<string>> list = new List<List<string>>();
			for (int i = 0; i < 2; i++)
			{
				List<string> list2 = new List<string>();
				for (int j = 0; j < 4; j++)
				{
					list2.Add(this.dgvRefrigerantPipe.Rows[i].Cells[j].Value.ToString());
				}
				list.Add(list2);
			}
			new ClassRefrigerantPipeCreateInfo().SetRefrigerantPipeInfo(list);
			string[] array = new string[1];
			if (this.rdbGasLiquidIntegration.Checked)
			{
				array[0] = "0";
			}
			else
			{
				array[0] = "1";
			}
			this.userSetting.WriteRefrigerantPipeCreateText(array);
		}

		private void FormRefrigerantPipeCreate_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				FormRefrigerantPipeCreate.SendMessage(RefrigerantPipeCreate.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void FormRefrigerantPipeCreate_Leave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				FormRefrigerantPipeCreate.SendMessage(RefrigerantPipeCreate.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void FormRefrigerantPipeCreate_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private void dgvRefrigerantPipe_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			this.cellValue = this.dgvRefrigerantPipe.CurrentCell.Value.ToString();
		}

		private void dgvRefrigerantPipe_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (this.dgvRefrigerantPipe.CurrentCell.Value == null)
			{
				this.dgvRefrigerantPipe.CurrentCell.Value = this.cellValue;
				YJKMessageBox.Show("输入不能为空！");
			}
			if (e.ColumnIndex == 2 && e.RowIndex < this.dgvRefrigerantPipe.RowCount && !this.regex.IsMatch(this.dgvRefrigerantPipe.CurrentCell.Value.ToString().Trim()))
			{
				this.dgvRefrigerantPipe.CurrentCell.Value = this.cellValue;
				YJKMessageBox.Show("输入数字格式不正确,应为整数或小数，且不能为空！");
			}
			if (e.ColumnIndex == 3 && e.RowIndex < this.dgvRefrigerantPipe.RowCount && !this.regex.IsMatch(this.dgvRefrigerantPipe.CurrentCell.Value.ToString().Trim()))
			{
				this.dgvRefrigerantPipe.CurrentCell.Value = this.cellValue;
				YJKMessageBox.Show("输入数字格式不正确,应为整数或小数，且不能为空！");
			}
			if (this.rdbGasLiquidIntegration.Checked)
			{
				this.dgvRefrigerantPipe.Rows[1].Cells[2].Value = this.dgvRefrigerantPipe.Rows[0].Cells[2].Value;
				this.dgvRefrigerantPipe.Rows[1].Cells[3].Value = this.dgvRefrigerantPipe.Rows[0].Cells[3].Value;
			}
			this.dgvRefrigerantPipe.CommitEdit(DataGridViewDataErrorContexts.Commit);
		}

		private Document doc;

		private bool gasLiquidIntegration = true;

		private HVACUserSetting userSetting = new HVACUserSetting();

		private string cellValue;

		private Regex regex = new Regex("^[+-]?[0-9]+\\d*[.]?\\d*$");
	}
}
